using System.Collections;
using UnityEngine;

public class FireBall : MonoBehaviour
{
	public ParticleSystem fireBurst;

	public ParticleSystem fragments;

	public ParticleSystem fireTrail;

	public AudioSource burstAudio;

	public LayerMask hitLayer;

	private void Start()
	{
		StartCoroutine(Destroy());
	}

	public void OnCollisionEnter(Collision col)
	{
		float sqrMagnitude = col.relativeVelocity.sqrMagnitude;
		if (sqrMagnitude < 10f)
		{
			return;
		}
		if (!burstAudio.isPlaying)
		{
			burstAudio.volume = 0.03f * sqrMagnitude;
			RandomizePitch(burstAudio);
			burstAudio.Play();
		}
		fireBurst.Play();
		fragments.Play();
		Vector3 position = base.transform.position;
		Collider[] array = Physics.OverlapSphere(position, 2f, hitLayer);
		for (int i = 0; i < array.Length; i++)
		{
			if ((bool)array[i] && (bool)array[i].GetComponent<Rigidbody>())
			{
				array[i].GetComponent<Rigidbody>().AddExplosionForce(25f, position, 2f, 2f, ForceMode.Impulse);
			}
		}
	}

	private void RandomizePitch(AudioSource audio)
	{
		audio.pitch = UnityEngine.Random.Range(0.75f, 1.2f);
	}

	private IEnumerator Destroy()
	{
		yield return new WaitForSeconds(GetComponent<Animation>().clip.length - 1f);
		fireTrail.enableEmission = false;
		yield return new WaitForSeconds(1f);
		UnityEngine.Object.Destroy(base.gameObject);
	}
}
